package com.baizan.daka.ui.view

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import com.baizan.daka.base.BaseLayoutView
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.view_actionbar.view.*

class ActionBar : BaseLayoutView {
    var mLeftIconShow: Boolean = true
        private set
    var mCenterTextShow: Boolean = true
    var mRightTextShow: Boolean = false
        private set

    //    var underLineShow: Boolean? = null
    var mCenterTextColor: Int = 0
    var mRightTextColor: Int = 0
    var mLeftIconResId: Int = 0

    var mCenterText: String = ""
    var mRightText: String = ""

    override fun initLayout(): Int {
        return R.layout.view_actionbar
    }


    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    fun setCenterText(text: String) {
        title_tv.text = text
    }

    fun setRightText(text: String) {
        right_tv.text = text
    }

    fun setLeftShow(show: Boolean) {
        mLeftIconShow = show
        left_iv.visibility = if (show) View.VISIBLE else View.GONE
    }

    fun setRightShow(show: Boolean) {
        mRightTextShow = show
        right_tv.visibility = if (mRightTextShow) View.VISIBLE else View.GONE
    }

    fun setLeftOnClickListener(l: OnClickListener) {
        left_iv.setOnClickListener(l)
    }

    fun setRightOnClickListener(l: OnClickListener) {
        right_tv.setOnClickListener(l)
    }

    override fun initView() {
        super.initView()
        setLeftShow(mLeftIconShow)
        setRightShow(mRightTextShow)
        if (mLeftIconResId > 0) {
            left_iv.setImageResource(mLeftIconResId)
        }
        if (mCenterTextColor > 0) {
            title_tv.setTextColor(mCenterTextColor)
        }


        if (mRightTextColor > 0) {
            right_tv.setTextColor(mRightTextColor)
        }
        title_tv.text = mCenterText
        right_tv.text = mRightText
        left_iv.setOnClickListener {
            if (context is Activity) {
                val a = context as Activity
                a.finish()
            }
        }
    }

    override fun initData() {
        super.initData()

    }

    fun init(attrs: AttributeSet) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.action_bar)
        mLeftIconShow = ta.getBoolean(R.styleable.action_bar_icon_left_show, true)
        mCenterTextShow = ta.getBoolean(R.styleable.action_bar_txt_center_show, true)
        mRightTextShow = ta.getBoolean(R.styleable.action_bar_txt_right_show, false)


        mCenterTextColor = ta.getColor(R.styleable.action_bar_text_center_color, Color.WHITE)
        mRightTextColor = ta.getColor(R.styleable.action_bar_text_right_color, Color.WHITE)
        mLeftIconResId = ta.getResourceId(R.styleable.action_bar_icon_left, 0)

        if (ta.hasValue(R.styleable.action_bar_text_center)) {
            var mCenterTextResId = ta.getResourceId(R.styleable.action_bar_text_center, 0)
            if (mCenterTextResId == 0) {
                mCenterText = ta.getString(R.styleable.action_bar_text_center)
            } else {
                mCenterText = context.getString(mCenterTextResId)
            }
        }

        if (ta.hasValue(R.styleable.action_bar_text_right)) {
            mRightTextShow = true
            var mRightTextResId = ta.getResourceId(R.styleable.action_bar_text_right, 0)
            if (mRightTextResId == 0) {
                mRightText = ta.getString(R.styleable.action_bar_text_right)
            } else {
                mRightText = context.getString(mRightTextResId)
            }
        }

//        underLineShow = ta.getBoolean(R.styleable.action_bar_under_line_show, false)
    }
}